package com.crazy.index.exception;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;


//@RestController
//@ControllerAdvice
public class BizExceptionHandler {
	
	private Map<Class<? extends Exception>, Integer> exceptionHandlerStatusCodes = new HashMap<>();
	{
		exceptionHandlerStatusCodes.put(NoSuchRequestHandlingMethodException.class, HttpServletResponse.SC_NOT_FOUND);
		exceptionHandlerStatusCodes.put(HttpRequestMethodNotSupportedException.class, HttpServletResponse.SC_METHOD_NOT_ALLOWED);
		exceptionHandlerStatusCodes.put(HttpMediaTypeNotSupportedException.class, HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
		exceptionHandlerStatusCodes.put(HttpMediaTypeNotAcceptableException.class, HttpServletResponse.SC_NOT_ACCEPTABLE);
		exceptionHandlerStatusCodes.put(MissingPathVariableException.class, HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		exceptionHandlerStatusCodes.put(MissingServletRequestParameterException.class, HttpServletResponse.SC_BAD_REQUEST);
		exceptionHandlerStatusCodes.put(ServletRequestBindingException.class, HttpServletResponse.SC_BAD_REQUEST);
		exceptionHandlerStatusCodes.put(ConversionNotSupportedException.class, HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		exceptionHandlerStatusCodes.put(TypeMismatchException.class, HttpServletResponse.SC_BAD_REQUEST);
		exceptionHandlerStatusCodes.put(HttpMessageNotReadableException.class, HttpServletResponse.SC_BAD_REQUEST);
		exceptionHandlerStatusCodes.put(HttpMessageNotWritableException.class, HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
		exceptionHandlerStatusCodes.put(MethodArgumentNotValidException.class, HttpServletResponse.SC_BAD_REQUEST);
		exceptionHandlerStatusCodes.put(MissingServletRequestPartException.class, HttpServletResponse.SC_BAD_REQUEST);
		exceptionHandlerStatusCodes.put(BindException.class, HttpServletResponse.SC_BAD_REQUEST);
		exceptionHandlerStatusCodes.put(NoHandlerFoundException.class, HttpServletResponse.SC_NOT_FOUND);
		exceptionHandlerStatusCodes.put(AsyncRequestTimeoutException.class, HttpServletResponse.SC_SERVICE_UNAVAILABLE);
	}
	
	@ExceptionHandler(value = Exception.class)
	public Object resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
			Exception ex, Map map) {
		
		if(ex.getClass() != HttpMediaTypeNotAcceptableException.class){
			//
			Integer status = exceptionHandlerStatusCodes.get(ex.getClass());
			status = status == null ? HttpServletResponse.SC_INTERNAL_SERVER_ERROR : status;
			
		}
		// 正常异常: HttpMediaTypeNotAcceptableException
		if(ex.getClass() == HttpMediaTypeNotAcceptableException.class){
			
		}
		return new ModelAndView();
	}
	
}
